package de.upb.aquarium.water;

import java.nio.ByteBuffer;

import javax.media.opengl.GL;

public class TextureRenderer {
	
	private int width, height;
	private int textureId;
	
	public TextureRenderer(GL gl, int width, int height) {
		
		this.width = width;
		this.height = height;

		int[] tmp = new int[1];
		gl.glGenTextures(1, tmp, 0);
		textureId = tmp[0];
		
		gl.glBindTexture(GL.GL_TEXTURE_2D, textureId);
		
		// dummy buffer:
		byte[] bytes = new byte[height*width*3];
		
		gl.glTexImage2D(
				GL.GL_TEXTURE_2D, 0, GL.GL_RGB,
				this.height, this.width, 0,	// Use border
				GL.GL_RGB, GL.GL_UNSIGNED_BYTE, ByteBuffer.wrap(bytes));

		gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
		gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
	}

	public void copyFrameBufferToTexture(GL gl)
	{
		gl.glBindTexture(GL.GL_TEXTURE_2D, textureId);
		gl.glCopyTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, 0, 0, height, width, 0);
	}

	public void bindTexture(GL gl)
	{
		gl.glBindTexture(GL.GL_TEXTURE_2D, textureId);
	}
	
	public int getTextureObject()
	{
		return textureId;
	}
}
